﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Omes.Services;
using CommonControl.StyleFashin.Web.CustomerControls;

namespace FashionMural.Web
{
    public partial class Servers : System.Web.UI.Page
    {
        private IPageService pageservice;
        private IResourceService resourceservice;
        public List<Omes.Model.Page> ChildPages { set; get; }
        public Omes.Model.Page CurrentPage { set; get; }
        public List<Omes.Model.NewsList> News { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            pageservice = new PageService();
            resourceservice = new ResourceService();
            var pageidstr = HttpContext.Current.Request.Params["t"];
            var pagenumberstr = HttpContext.Current.Request.Params["p"];
            //获取项目子页面
            int sid = int.Parse(System.Configuration.ConfigurationManager.AppSettings["fwxm"]);
            ChildPages = pageservice.GetChildPage(sid);
            int pageid = 0;
            int pageNumber = 1;
            if (!int.TryParse(pagenumberstr, out pageNumber))
            {
                pageNumber = 1;
            }
            int nid = int.Parse(System.Configuration.ConfigurationManager.AppSettings["xwzx"]);
            News = resourceservice.GetNews(nid, commend: 1).Lists;
            if (!int.TryParse(pageidstr, out pageid))
            {
                pageid = ChildPages[0].Id;
            }
                var page = pageservice.GetPage(pageid);
                var controlPartName = string.Empty;
                CurrentPage = pageservice.GetPage(pageid);
                if (page != null)
                {
                    switch (page.PageTypeId)
                    {
                        case 2: controlPartName = "ListImagesControl"; break;
                        case 0: controlPartName = "HtmlControl"; break;
                        case 1: controlPartName = "ListLinksControl"; break;
                    }
                }
                else
                {

                }
                //获取第一页图片列表
                var customControl = this.Page.LoadControl("/StyleFashin/" + controlPartName + ".ascx") as CommonControl.Web.CustomerControls.ICustomControl;
                customControl.ItemAction = "/ServerDetail.aspx?t={0}&f=" + pageid;
                customControl.PageUrl = "/Servers.aspx?t=" + pageid + "&p={0}";
                if (controlPartName == "ListImagesControl")
                {
                    var imgcontrol = customControl as CommonControl.IListImageControl;
                    var imgp = resourceservice.GetImages(pageid, null, pageNumber, 9);
                    imgcontrol.Images = imgp.Images;
                    imgcontrol.PageSize = 9;
                    imgcontrol.TotalNumber = (imgp.TotalCount + 8) / 9;
                    imgcontrol.CurrentPageNumber = pageNumber;
                }
                else if (controlPartName == "ListLinksControl")
                {
                    var lscl = customControl as CommonControl.IListTextControl;
                    var ls = resourceservice.GetNews(pageid, null, pageNumber, 9);
                    lscl.PageSize = 9;
                    lscl.TotalNumber = (ls.TotalCount + 8) / 9;
                    lscl.CurrentPageNumber = pageNumber;
                }
                else if (controlPartName == "HtmlControl")
                {
                    var htmlcontrol = customControl as CommonControl.IHTMLControl;
                    var html = resourceservice.GetHtml(pageid);
                    htmlcontrol.HtmlText = html.Content;
                }
                contentPH.Controls.Add(customControl as UserControl);
            
        }
    }
}